package com.zhuangfei.timetable.view;

import com.zhuangfei.timetable.ResourceTable;
import com.zhuangfei.timetable.listener.IWeekView;
import com.zhuangfei.timetable.listener.OnWeekItemClickedAdapter;
import com.zhuangfei.timetable.listener.OnWeekLeftClickedAdapter;
import com.zhuangfei.timetable.model.Schedule;
import com.zhuangfei.timetable.model.ScheduleEnable;
import com.zhuangfei.timetable.model.ScheduleSupport;
import com.zhuangfei.timetable.model.WeekViewEnable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * 周次选择栏自定义View.
 * 每一项均为PerWeekView<br/>
 */
public class WeekView extends DirectionalLayout implements WeekViewEnable<WeekView> {
    private static final String TAG = "WeekView";
    private static final HiLogLabel LOG_LABEL = new HiLogLabel(3, 0, TAG);
    LayoutScatter mInflate;

    /**
     * 周次的容器
     */
    DirectionalLayout container;

    /**
     * 跟布局
     */
    DirectionalLayout root;

    /**
     * 左侧按钮
     */
    DirectionalLayout leftlayout;

    Text updateText;

    /**
     * 数据
     */
    private List<Schedule> dataSource;

    /**
     * 布局保存
     *
     */
    private List<DirectionalLayout> layouts;
    private List<Text> textViews;

    /**
     * 当前周
     */
    private int curWeek = 1;
    private int preIndex = 1;

    /**
     * 多少项
     */
    private int itemCount = 20;

    private IWeekView.OnWeekItemClickedListener onWeekItemClickedListener;
    private IWeekView.OnWeekLeftClickedListener onWeekLeftClickedListener;

    /**
     * WeekView
     *
     * @param context
     */
    public WeekView(Context context) {
        this(context, null);
    }

    /**
     * 获取Item点击监听
     *
     * @return OnWeekItemClickedListener
     */
    public IWeekView.OnWeekItemClickedListener onWeekItemClickedListener() {
        if (onWeekItemClickedListener == null) {
            onWeekItemClickedListener = new OnWeekItemClickedAdapter();
        }
        return onWeekItemClickedListener;
    }

    /**
     * 设置Item点击监听
     *
     * @param onWeekItemClickedListener
     * @return WeekView
     */
    public WeekView callback(IWeekView.OnWeekItemClickedListener onWeekItemClickedListener) {
        this.onWeekItemClickedListener = onWeekItemClickedListener;
        return this;
    }

    /**
     * 获取左侧按钮点击监听
     *
     * @return OnWeekLeftClickedListener
     */
    public IWeekView.OnWeekLeftClickedListener onWeekLeftClickedListener() {
        if (onWeekLeftClickedListener == null) {
            onWeekLeftClickedListener = new OnWeekLeftClickedAdapter();
        }
        return onWeekLeftClickedListener;
    }

    /**
     * 设置左侧按钮点击监听
     *
     * @param onWeekLeftClickedListener 监听
     * @return WeekView
     */
    public WeekView callback(IWeekView.OnWeekLeftClickedListener onWeekLeftClickedListener) {
        this.onWeekLeftClickedListener = onWeekLeftClickedListener;
        return this;
    }

    /**
     * 设置当前周
     *
     * @param curWeek 当前周
     * @return WeekView
     */
    @Override
    public WeekView curWeek(int curWeek) {
        if (curWeek < 1) {
            curWeek = 1;
        }
        this.curWeek = curWeek;
        return this;
    }

    /**
     * 设置项数
     *
     * @param count 项数
     * @return WeekView
     */
    @Override
    public WeekView itemCount(int count) {
        if (count <= 0) {
            return this;
        }
        this.itemCount = count;
        return this;
    }

    @Override
    public int itemCount() {
        return itemCount;
    }

    /**
     * 设置数据源
     *
     * @param list 数据集
     * @return WeekView
     */
    @Override
    public WeekView source(List<? extends ScheduleEnable> list) {
        data(ScheduleSupport.transform(list));
        return this;
    }

    /**
     * 设置数据源
     *
     * @param scheduleList 数据源
     * @return WeekView
     */
    @Override
    public WeekView data(List<Schedule> scheduleList) {
        if (scheduleList == null) {
            return null;
        }
        this.dataSource = scheduleList;
        return this;
    }

    /**
     * 获取数据源
     *
     * @return 数据集
     */
    @Override
    public List<Schedule> dataSource() {
        if (dataSource == null) {
            dataSource = new ArrayList<>();
        }
        return dataSource;
    }

    /**
     * WeekView
     *
     * @param context
     * @param attrs
     */
    public WeekView(Context context, AttrSet attrs) {
        super(context, attrs);
        mInflate = LayoutScatter.getInstance(context);
        initView();
    }

    private void initView() {
        Component view = mInflate.parse(ResourceTable.Layout_view_weekview, this, true);
        container = (DirectionalLayout) view.findComponentById(ResourceTable.Id_id_weekview_container);
        root = (DirectionalLayout) view.findComponentById(ResourceTable.Id_id_root);
        leftlayout = (DirectionalLayout) view.findComponentById(ResourceTable.Id_id_weekview_leftlayout);
        updateText = (Text) view.findComponentById(ResourceTable.Id_id_update_week);

        updateText.setText("  修改  \n当前周");
    }

    /**
     * 初次构建时调用，显示周次选择布局
     *
     * @return WeekView
     */
    @Override
    public WeekView showView() {
        if (curWeek < 1) {
            curWeek(1);
        }
        if (curWeek > itemCount()) {
            curWeek = itemCount;
        }
        getContext().getUITaskDispatcher().asyncDispatch(() -> {
            container.removeAllComponents();
        });

        layouts = new ArrayList<>();
        textViews = new ArrayList<>();

        leftlayout.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                onWeekLeftClickedListener().onWeekLeftClicked();
            }
        });
        for (int i = 1; i <= itemCount; i++) {
            final int tmp = i;
            Component view = mInflate.parse(ResourceTable.Layout_item_weekview, null, true);
            final DirectionalLayout perLayout = (DirectionalLayout) view.findComponentById(
                    ResourceTable.Id_id_perweekview_layout);
            Text weekText = (Text) view.findComponentById(ResourceTable.Id_id_weektext);
            Text bottomText = (Text) view.findComponentById(ResourceTable.Id_id_weektext_bottom);

            weekText.setText("第" + i + "周");
            if (i == curWeek) {
                bottomText.setText("(本周)");
            }
            PerWeekView perWeekView = (PerWeekView) view.findComponentById(ResourceTable.Id_id_perweekview);
            perWeekView.setData(dataSource(), i);
            perLayout.setClickedListener(new ClickedListener() {
                @Override
                public void onClick(Component component) {
                    resetBackground();
                    preIndex = tmp;
                    ShapeElement element = new ShapeElement();
                    element.setRgbColor(new RgbColor(255,255,255));
                    element.setCornerRadius(3);
                    perLayout.setBackground(element);
                    onWeekItemClickedListener().onWeekClicked(tmp);
                }
            });

            layouts.add(perLayout);
            textViews.add(bottomText);
            getContext().getUITaskDispatcher().asyncDispatch(() -> {
                container.addComponent(view);
            });
        }
        if (curWeek > 0 && curWeek <= layouts.size()) {
            ShapeElement element = new ShapeElement();
            element.setCornerRadius(3);
            element.setRgbColor(new RgbColor(183, 245, 242));
            layouts.get(curWeek - 1).setBackground(element);
        }
        return this;
    }

    /**
     * 当前周被改变后可以调用该方式修正一下底部的文本
     *
     * @return WeekView
     */
    @Override
    public WeekView updateView() {
        if (layouts == null || layouts.size() == 0) {
            return this;
        }
        if (textViews == null || textViews.size() == 0) {
            return this;
        }
        for (int i = 0; i < layouts.size(); i++) {
            if (i == curWeek - 1) {
                textViews.get(i).setText("(本周)");
            } else {
                textViews.get(i).setText("");
            }
            ShapeElement element = new ShapeElement();
            element.setRgbColor(new RgbColor(226,247,246));
            layouts.get(i).setBackground(element);
        }
        if (curWeek > 0 && curWeek <= layouts.size()) {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(new RgbColor(183,245,242));
            element.setCornerRadius(3);
            layouts.get(curWeek - 1).setBackground(element);
        }
        return this;
    }

    /**
     * 重置背景色
     */
    public void resetBackground() {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(226, 247, 246));
        layouts.get(preIndex - 1).setBackground(shapeElement);

        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(183,245,242));
        element.setCornerRadius(3);
        layouts.get(curWeek - 1).setBackground(element);
    }

    /**
     * 隐藏左侧设置当前周的控件
     *
     * @return WeekView
     */
    public WeekView hideLeftLayout() {
        leftlayout.setVisibility(HIDE);
        return this;
    }

    /**
     * 设置控件的可见性
     *
     * @param isShow true:显示，false:隐藏
     * @return WeekView
     */
    @Override
    public WeekView isShow(boolean isShow) {
        if (isShow) {
            root.setVisibility(VISIBLE);
        } else {
            root.setVisibility(HIDE);
        }
        return this;
    }

    /**
     * 判断该控件是否显示
     *
     * @return 是否显示
     */
    @Override
    public boolean isShowing() {
        if (root.getVisibility() == HIDE) {
            return false;
        }
        return true;
    }

    private void debug(String message) {
        HiLog.debug(LOG_LABEL, message);
    }
}
